// api/routes/user_routes.go
package routes

import (
	"mygin/api/handlers"
	"mygin/api/middleware"
	"mygin/config"
	"mygin/internal/repositories"
	"mygin/internal/services"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func RegisterUserRoutes(r *gin.RouterGroup, db *gorm.DB) {
	userRepo := repositories.NewUserRepository(db)
	userService := services.NewUserService(userRepo)
	userHandler := handlers.NewUserHandler(userService)

	// 初始化认证服务
	jwtCfg := config.LoadJWTConfig()
	authRepo := repositories.NewAuthRepository(db)
	authService := services.NewAuthService(jwtCfg, authRepo)

	userRoutes := r.Group("/users")
	{
		userRoutes.POST("/", userHandler.CreateUser)
		//userRoutes.GET("/:id", userHandler.GetUser)
		userRoutes.GET("/", userHandler.ListUsers)
		userRoutes.PUT("/:id", userHandler.UpdateUser)
		userRoutes.PATCH("/:id", userHandler.UpdateUser)
		userRoutes.DELETE("/:id", userHandler.DeleteUser)

		userRoutes.GET("/g", userHandler.GetExternalData)
		userRoutes.GET("/p", userHandler.CreateExternalData)

		// 需要认证的路由组
		secured := userRoutes.Group("/")
		secured.Use(middleware.JWTAuthMiddleware(authService))
		{
			secured.GET("/:id", userHandler.GetUser)
		}
		// 其他user路由...
		userRoutes.GET("/username/:username", userHandler.GetUserByUsername)
	}

}

func RegisterUserRoutesV2(r *gin.RouterGroup, db *gorm.DB) {
	// V2版本的用户路由
	userRepo := repositories.NewUserRepository(db)
	userService := services.NewUserService(userRepo)
	userHandler := handlers.NewUserHandler(userService)

	// 需要认证的路由组
	authRoutes := r.Group("/users")
	authRoutes.Use(middleware.LoggerMiddleware()) // 应用认证中间件
	{
		// 用户列表（需要认证）
		authRoutes.GET("/users_order/:id", userHandler.UserWithOrders)

		// 其他需要认证的路由...
		// authRoutes.POST("/users/:id/avatar", userHandler.UploadAvatar)
	}
	//r.POST("/users", userHandler.CreateUserV2)
	// 其他V2特有的user路由...
}
